﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace WeFrame.AutoCode
{
    class UILHelper
    {
        /// <summary>
        /// 构造列表aspx文本
        /// </summary>
        /// <param name="tableEntity"></param>
        /// <param name="columnList"></param>
        /// <returns></returns>

        public string GenerateListAspxText(TableEntity tableEntity, List<ColumnEntity> columnList, ConfigParameter configParameter)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"" + tableEntity.TableName + "List.aspx.cs\" Inherits=\""+GetNameSpace(configParameter.NameSpaceTag) +"."+ tableEntity.TableName + "List\" %>");
            sb.AppendLine("<%@ Import namespace=\"WeFrame.Core.Common\" %>");
            sb.AppendLine("<%@ Register assembly=\"AspNetPager\" namespace=\"Wuqi.Webdiyer\" tagprefix=\"webdiyer\" %>");
            sb.AppendLine("");
            sb.AppendLine("<!DOCTYPE html>");
            sb.AppendLine("<html>");
            sb.AppendLine("<head>");
            sb.AppendLine("    <meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            sb.AppendLine("    <meta name=\"viewport\" content=\"width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no\" />");
            sb.AppendLine("    <meta name=\"apple-mobile-web-app-capable\" content=\"yes\" />");
            sb.AppendLine("    <title>"+tableEntity.Description+"</title>");
            sb.AppendLine("    <link href=\"../Library/artdialog/ui-dialog.css\" rel=\"stylesheet\" type=\"text/css\" />");
            sb.AppendLine("    <link href=\"../Theme/default/style.css\" rel=\"stylesheet\" type=\"text/css\" />");
            sb.AppendLine("    <script type=\"text/javascript\" src=\"../Library/jquery/jquery-1.11.2.min.js\"></script>");
            sb.AppendLine("    <script type=\"text/javascript\" src=\"../Library/artdialog/dialog-plus-min.js\"></script>");
            sb.AppendLine("    <script type=\"text/javascript\" charset=\"utf-8\" src=\"../JavaScript/laymain.js\"></script>");
            sb.AppendLine("    <script type=\"text/javascript\" charset=\"utf-8\" src=\"../JavaScript/common.js\"></script>");
            sb.AppendLine("</head>");
            sb.AppendLine("");
            sb.AppendLine("<body class=\"mainbody\">");
            sb.AppendLine("<form id=\"form1\" runat=\"server\">");
            sb.AppendLine("<!--导航栏-->");
            sb.AppendLine("<div class=\"location\">");
            sb.AppendLine("  <a href=\"javascript:history.back(-1);\" class=\"back\"><i></i><span>返回上一页</span></a>");
            sb.AppendLine("  <a href=\"../center.aspx\" class=\"home\"><i></i><span>首页</span></a>");
            sb.AppendLine("  <i class=\"arrow\"></i>");
            sb.AppendLine("  <span>" + tableEntity.Description + "列表</span>");
            sb.AppendLine("</div>");
            sb.AppendLine("<!--/导航栏-->");
            sb.AppendLine("");
            sb.AppendLine("<!--工具栏-->");
            sb.AppendLine("<div id=\"floatHead\" class=\"toolbar-wrap\">");
            sb.AppendLine("  <div class=\"toolbar\">");
            sb.AppendLine("    <div class=\"box-wrap\">");
            sb.AppendLine("      <a class=\"menu-btn\"></a>");
            sb.AppendLine("      <div class=\"l-list\">");
            sb.AppendLine("        <ul class=\"icon-list\">");
            sb.AppendLine("          <li><a class=\"add\" href=\"" + tableEntity.TableName + "Edit.aspx?action=<%=WeFrameEnums.ActionEnum.Add %>\"><i></i><span>新增</span></a></li>");
            sb.AppendLine("          <li><a class=\"all\" href=\"javascript:;\" onclick=\"checkAll(this);\"><i></i><span>全选</span></a></li>");
            sb.AppendLine("          <li><asp:LinkButton ID=\"LinkButtonDelete\" runat=\"server\" CssClass=\"del\" ");
            sb.AppendLine("                  OnClientClick=\"return ExePostBack('LinkButtonDelete');\" ");
            sb.AppendLine("                  onclick=\"LinkButtonDelete_Click\"><i></i><span>删除</span></asp:LinkButton></li>");
            sb.AppendLine("        </ul>");
            sb.AppendLine("      </div>");
            sb.AppendLine("      <div class=\"r-list\">");
            sb.AppendLine("        <asp:TextBox ID=\"TextBoxKeyword\" runat=\"server\" CssClass=\"keyword\" />");
            sb.AppendLine("        <asp:LinkButton ID=\"LinkButtonSearch\" runat=\"server\" CssClass=\"btn-search\" onclick=\"LinkButtonSearch_Click\">查询</asp:LinkButton>");
            sb.AppendLine("      </div>");
            sb.AppendLine("    </div>");
            sb.AppendLine("  </div>");
            sb.AppendLine("</div>");
            sb.AppendLine("<!--/工具栏-->");
            sb.AppendLine("");
            sb.AppendLine("<!--列表-->");
            sb.AppendLine("<div class=\"table-container\">");
            sb.AppendLine("  <asp:Repeater ID=\"RepeaterList\" runat=\"server\">");
            sb.AppendLine("  <HeaderTemplate>");
            sb.AppendLine("  <table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"ltable\">");
            sb.AppendLine("    <tr>");
            sb.AppendLine("      <th width=\"8%\">选择</th>");


            foreach (ColumnEntity column in columnList)
            {
            sb.AppendLine("      <th align=\"left\">"+column.Description+"</th>");
            }

            
            sb.AppendLine("      <th>操作</th>");
            sb.AppendLine("    </tr>");
            sb.AppendLine("  </HeaderTemplate>");
            sb.AppendLine("  <ItemTemplate>");
            sb.AppendLine("    <tr>");
            sb.AppendLine("      <td align=\"center\">");
            sb.AppendLine("        <asp:CheckBox ID=\"CheckBoxId\" CssClass=\"checkall\" runat=\"server\" style=\"vertical-align:middle;\" />");
            sb.AppendLine("        <asp:HiddenField ID=\"HiddenFieldId\" Value='<%#Eval(\"Id\")%>' runat=\"server\" />");
            sb.AppendLine("      </td>");
           

            foreach (ColumnEntity column in columnList)
            {
            sb.AppendLine("      <td><%# Eval(\""+column.Name+"\") %></td>");
            }

            sb.AppendLine("      <td align=\"center\"><a href=\"" + tableEntity.TableName + "Edit.aspx?action=<%#WeFrameEnums.ActionEnum.Edit %>&id=<%#Eval(\"Id\")%>\">修改</a></td>");
            sb.AppendLine("    </tr>");
            sb.AppendLine("  </ItemTemplate>");
            sb.AppendLine("  <FooterTemplate>");
            sb.AppendLine("    <%#RepeaterList.Items.Count == 0 ? \"<tr><td align=\\\"center\\\" colspan=\\\"8\\\">暂无记录</td></tr>\" : \"\"%>");
            sb.AppendLine("  </table>");
            sb.AppendLine("  </FooterTemplate>");
            sb.AppendLine("  </asp:Repeater>");
            sb.AppendLine("</div>");
            sb.AppendLine("<!--/列表-->");

            sb.AppendLine("<!--内容底部-->");
            sb.AppendLine("<div class=\"line20\"></div>");
            sb.AppendLine("<div class=\"pagelist\">");
            sb.AppendLine("  <div class=\"l-btns\">");
            sb.AppendLine("    <span>显示</span><asp:TextBox ID=\"TextBoxPageSize\" runat=\"server\" ");
            sb.AppendLine("          CssClass=\"pagenum\" onkeydown=\"return checkNumber(event);\"");
            sb.AppendLine("                OnTextChanged=\"TextBoxPageSize_TextChanged\" AutoPostBack=\"True\" ");
            sb.AppendLine("          TextMode=\"Number\">10</asp:TextBox><span>条/页</span>");
            sb.AppendLine("  </div>");
            sb.AppendLine("  <div>");
            sb.AppendLine("      <webdiyer:AspNetPager ID=\"AspNetPager\" runat=\"server\" ");
            sb.AppendLine("        onpagechanged=\"AspNetPager_PageChanged\" CssClass=\"anpager\" ");
            sb.AppendLine("        CurrentPageButtonClass=\"cpb\" LayoutType=\"Ul\" PagingButtonSpacing=\"0px\" ");
            sb.AppendLine("        FirstPageText=\"首页\" LastPageText=\"尾页\" NextPageText=\"下一页\" PrevPageText=\"上一页\"></webdiyer:AspNetPager>");
            sb.AppendLine("  </div>");
            sb.AppendLine("</div>");
            sb.AppendLine("");
            sb.AppendLine("<!--/内容底部-->");
            sb.AppendLine("</form>");
            sb.AppendLine("</body>");
            sb.AppendLine("</html>");
            return sb.ToString();
        
        }

        /// <summary>
        /// 构造列表aspx.cs文件
        /// </summary>
        /// <param name="tableEntity"></param>
        /// <param name="columnList"></param>
        /// <returns></returns>
        public  string GenerateListAspxCsText(TableEntity tableEntity, List<ColumnEntity> columnList,ConfigParameter configParameter)
        {
            
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("using System;");
            sb.AppendLine("using System.Text;");
            sb.AppendLine("using System.Data;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Web;");
            sb.AppendLine("using System.Web.UI;");
            sb.AppendLine("using System.Web.UI.WebControls;");
            sb.AppendLine("using WeFrame.Core;");
            sb.AppendLine("using WeFrame.Core.Common;");
            sb.AppendLine("using WeFrame.Core.Model;");
            sb.AppendLine("using " + configParameter.NameSpaceTag + ".Model;");
            sb.AppendLine("using " + configParameter.NameSpaceTag + ".BLL;");
            sb.AppendLine("");
            sb.AppendLine("namespace "+GetNameSpace(configParameter.NameSpaceTag));
            sb.AppendLine("{");
            sb.AppendLine("    public partial class " + tableEntity.TableName + "List :  WeFrame.Core.UI.AdminPage");
            sb.AppendLine("    {");
            sb.AppendLine("");
            sb.AppendLine("        " + tableEntity.TableName + "BLL bll = new " + tableEntity.TableName + "BLL();");
            sb.AppendLine("        protected List<QueryParam>  queryParamList = new List<QueryParam>();");
            sb.AppendLine("        protected List<QueryOrder> queryOrderList  = new List<QueryOrder>();");
            sb.AppendLine("        protected QueryPage queryPage = new QueryPage();");
            sb.AppendLine("");
            sb.AppendLine("        protected void Page_Load(object sender, EventArgs e)");
            sb.AppendLine("        {");
            sb.AppendLine("            ChkAdminLevel(\"" + tableEntity.TableName + "\", WeFrameEnums.ActionEnum.View.ToString()); //检查权限");
            sb.AppendLine("            ");
            sb.AppendLine("            if (!Page.IsPostBack)");
            sb.AppendLine("            {");
            sb.AppendLine("                PageDataBind();");
            sb.AppendLine("            }");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 关健字查询");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"sender\"></param>");
            sb.AppendLine("        /// <param name=\"e\"></param>");
            sb.AppendLine("        protected void LinkButtonSearch_Click(object sender, EventArgs e)");
            sb.AppendLine("        {");
            sb.AppendLine("            queryPage.PageIndex = 1;");
            sb.AppendLine("            PageDataBind();");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 设置分页数量");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"sender\"></param>");
            sb.AppendLine("        /// <param name=\"e\"></param>");
            sb.AppendLine("        protected void TextBoxPageSize_TextChanged(object sender, EventArgs e)");
            sb.AppendLine("        {");
            sb.AppendLine("            queryPage.PageSize = int.Parse(TextBoxPageSize.Text);");
            sb.AppendLine("            PageDataBind();");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 转到相应页面");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"sender\"></param>");
            sb.AppendLine("        /// <param name=\"e\"></param>");
            sb.AppendLine("        protected void AspNetPager_PageChanged(object sender, EventArgs e)");
            sb.AppendLine("        {");
            sb.AppendLine("            queryPage.PageIndex = AspNetPager.CurrentPageIndex;");
            sb.AppendLine("            PageDataBind();");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 页面数据绑定");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        protected void PageDataBind() ");
            sb.AppendLine("        {");
            sb.AppendLine("            BuildQueryPage();");
            sb.AppendLine("            BuildQueryParam(TextBoxKeyword.Text.Trim());");
            sb.AppendLine("            BuildQueryOrder();");
            sb.AppendLine("            RepeaterDataBind();");
            sb.AppendLine("            AspNetPagerDataBind();");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        ///  数据绑定");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        private void RepeaterDataBind()");
            sb.AppendLine("        {");
            sb.AppendLine("");
            sb.AppendLine("            RepeaterList.DataSource = bll.QueryDataSet(queryParamList, queryOrderList, queryPage);");
            sb.AppendLine("            RepeaterList.DataBind();");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 分页绑定");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        private void AspNetPagerDataBind()");
            sb.AppendLine("        {");
            sb.AppendLine("            AspNetPager.RecordCount = queryPage.ObjectCount;");
            sb.AppendLine("            AspNetPager.CurrentPageIndex = queryPage.PageIndex;");
            sb.AppendLine("            AspNetPager.PageSize = queryPage.PageSize;");
            sb.AppendLine("            AspNetPager.DataBind();");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 构建查询条件");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <param name=\"keywords\"></param>");
            sb.AppendLine("        protected void BuildQueryParam(string keywords)");
            sb.AppendLine("        {");
            sb.AppendLine("            QueryParam name = new QueryParam();");
            sb.AppendLine("            name.ParamName = \"Name\";");
            sb.AppendLine("            name.ParamValue = \"%\" + keywords + \"%\";");
            sb.AppendLine("            name.Operation = \" like \";");
            sb.AppendLine("            queryParamList.Add(name);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 构造排序条件");
            sb.AppendLine("        /// </summary> ");
            sb.AppendLine("        protected void BuildQueryOrder()");
            sb.AppendLine("        {");
            sb.AppendLine("            QueryOrder queryOrder = new QueryOrder();");
            sb.AppendLine("            queryOrder.OrderName = \"Id\";");
            sb.AppendLine("            queryOrder.OrderWay = \"Desc\";");
            sb.AppendLine("            queryOrderList.Add(queryOrder);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// 构造分页");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        protected void BuildQueryPage()");
            sb.AppendLine("        {");
            sb.AppendLine("            queryPage.PageSize = int.Parse(TextBoxPageSize.Text);");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("        //批量删除");
            sb.AppendLine("        protected void LinkButtonDelete_Click(object sender, EventArgs e)");
            sb.AppendLine("        {");
            sb.AppendLine("            ChkAdminLevel(\"" + tableEntity.TableName + "\", WeFrameEnums.ActionEnum.Delete.ToString()); //检查权限");
            sb.AppendLine("            bll.Delete(GetCheckedIds(RepeaterList));");
            sb.AppendLine("            PageDataBind();");
            sb.AppendLine("          ");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("    }");
            sb.AppendLine("}");


            return sb.ToString();
        }


        /// <summary>
        /// 构造列表aspx.designer.cs文件
        /// </summary>
        /// <param name="tableEntity"></param>
        /// <param name="columnList"></param>
        /// <returns></returns>
        public string GenerateListAspxDesignerCsText(TableEntity tableEntity, List<ColumnEntity> columnList,ConfigParameter configParameter)
        {
            
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("//------------------------------------------------------------------------------");
            sb.AppendLine("// <自动生成>");
            sb.AppendLine("//     此代码由工具生成。");
            sb.AppendLine("//");
            sb.AppendLine("//     对此文件的更改可能会导致不正确的行为，并且如果");
            sb.AppendLine("//     重新生成代码，这些更改将会丢失。"); 
            sb.AppendLine("// </自动生成>");
            sb.AppendLine("//------------------------------------------------------------------------------");
            sb.AppendLine("");
            sb.AppendLine("namespace "+GetNameSpace(configParameter.NameSpaceTag)+" {");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("    public partial class "+tableEntity.TableName+"List {");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// form1 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.HtmlControls.HtmlForm form1;");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// LinkButtonDelete 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.WebControls.LinkButton LinkButtonDelete;");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// TextBoxKeyword 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.WebControls.TextBox TextBoxKeyword;");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// LinkButtonSearch 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.WebControls.LinkButton LinkButtonSearch;");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// RepeaterList 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.WebControls.Repeater RepeaterList;");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// TextBoxPageSize 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.WebControls.TextBox TextBoxPageSize;");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// AspNetPager 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::Wuqi.Webdiyer.AspNetPager AspNetPager;");
            sb.AppendLine("    }");
            sb.AppendLine("}");

            return sb.ToString();
        }

        /// <summary>
        /// 构造编辑aspx文本
        /// </summary>
        /// <param name="tableEntity"></param>
        /// <param name="columnList"></param>
        /// <returns></returns>
        public string GenerateEditAspxText(TableEntity tableEntity, List<ColumnEntity> columnList,ConfigParameter configParameter)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("<%@ Page Language=\"C#\" AutoEventWireup=\"true\" CodeBehind=\"" + tableEntity.TableName + "Edit.aspx.cs\" Inherits=\""+GetNameSpace(configParameter.NameSpaceTag) +"."+ tableEntity.TableName + "Edit\" ValidateRequest=\"false\" %>");
            sb.AppendLine("<%@ Import namespace=\"WeFrame.Core.Common\" %>");
            sb.AppendLine("");
            sb.AppendLine("<!DOCTYPE html>");
            sb.AppendLine("<html>");
            sb.AppendLine("<head>");
            sb.AppendLine("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" />");
            sb.AppendLine("<meta name=\"viewport\" content=\"width=device-width,minimum-scale=1.0,maximum-scale=1.0,initial-scale=1.0,user-scalable=no\" />");
            sb.AppendLine("<meta name=\"apple-mobile-web-app-capable\" content=\"yes\" />");
            sb.AppendLine("<title>编辑"+tableEntity.Description+"</title>");
            sb.AppendLine("<link href=\"../Library/artdialog/ui-dialog.css\" rel=\"stylesheet\" type=\"text/css\" />");
            sb.AppendLine("<link href=\"../Theme/Default/style.css\" rel=\"stylesheet\" type=\"text/css\" />");
            sb.AppendLine("<script type=\"text/javascript\" src=\"../Library/jquery/jquery-1.11.2.min.js\"></script>");
            sb.AppendLine("<script type=\"text/javascript\" src=\"../Library/jquery/Validform_v5.3.2_min.js\"></script>");
            sb.AppendLine("<script type=\"text/javascript\" src=\"../Library/artdialog/dialog-plus-min.js\"></script>");
            sb.AppendLine("<script type=\"text/javascript\" charset=\"utf-8\" src=\"../JavaScript/laymain.js\"></script>");
            sb.AppendLine("<script type=\"text/javascript\" charset=\"utf-8\" src=\"../JavaScript/common.js\"></script>");
            sb.AppendLine("<script type=\"text/javascript\">");
            sb.AppendLine("    $(function () {");
            sb.AppendLine("        //初始化表单验证");
            sb.AppendLine("        $(\"#form1\").initValidform();");
            sb.AppendLine("    });");
            sb.AppendLine("</script>");
            sb.AppendLine("</head>");
            sb.AppendLine("");
            sb.AppendLine("<body class=\"mainbody\">");
            sb.AppendLine("<form id=\"form1\" runat=\"server\">");
            sb.AppendLine("<!--导航栏-->");
            sb.AppendLine("<div class=\"location\">");
            sb.AppendLine("  <a href=\""+tableEntity.TableName+"List.aspx\" class=\"back\"><i></i><span>返回列表页</span></a>");
            sb.AppendLine("  <a href=\"../center.aspx\" class=\"home\"><i></i><span>首页</span></a>");
            sb.AppendLine("  <i class=\"arrow\"></i>");
            sb.AppendLine("  <a href=\""+tableEntity.TableName+"List.aspx\"><span>"+tableEntity.Description+"</span></a>");
            sb.AppendLine("  <i class=\"arrow\"></i>");
            sb.AppendLine("  <span>编辑"+tableEntity.Description+"</span>");
            sb.AppendLine("</div>");
            sb.AppendLine("<div class=\"line10\"></div>");
            sb.AppendLine("<!--/导航栏-->");
            sb.AppendLine("");
            sb.AppendLine("<!--内容-->");
            sb.AppendLine("<div id=\"floatHead\" class=\"content-tab-wrap\">");
            sb.AppendLine("  <div class=\"content-tab\">");
            sb.AppendLine("    <div class=\"content-tab-ul-wrap\">");
            sb.AppendLine("      <ul>");
            sb.AppendLine("        <li><a class=\"selected\" href=\"javascript:;\">"+tableEntity.Description+"信息</a></li>");
            sb.AppendLine("      </ul>");
            sb.AppendLine("    </div>");
            sb.AppendLine("  </div>");
            sb.AppendLine("</div>");
            sb.AppendLine("");
            sb.AppendLine("<div class=\"tab-content\">");
            
            foreach (ColumnEntity column in columnList)
            {
                if ("Id".Equals(column.Name) || "CreateBy".Equals(column.Name) || "CreateTime".Equals(column.Name) || "ModifyBy".Equals(column.Name) || "ModifyTime".Equals(column.Name))
                {
                    continue;
                }
               
                sb.AppendLine("  <dl>");
                sb.AppendLine("    <dt>" + column.Description + "</dt>");
                sb.AppendLine("    <dd><asp:TextBox ID=\"TextBox" + column.Name + "\" runat=\"server\" CssClass=\"input normal\" ></asp:TextBox> </dd>");
                sb.AppendLine("  </dl>");
                
            }

            sb.AppendLine("</div>");
            sb.AppendLine("<!--/内容-->");
            sb.AppendLine("");
            sb.AppendLine("<!--工具栏-->");
            sb.AppendLine("<div class=\"page-footer\">");
            sb.AppendLine("  <div class=\"btn-wrap\">");
            sb.AppendLine("    <asp:Button ID=\"ButtonSubmit\" runat=\"server\" Text=\"提交保存\" CssClass=\"btn\" onclick=\"ButtonSubmit_Click\" />");
            sb.AppendLine("    <asp:HiddenField ID=\"HiddenFieldAction\"  runat=\"server\"/>");
            sb.AppendLine("    <asp:HiddenField ID=\"HiddenFieldId\"   runat=\"server\"/>");
            sb.AppendLine("    <input name=\"btnReturn\" type=\"button\" value=\"返回上一页\" class=\"btn yellow\" onclick=\"javascript:history.back(-1);\" />");
            sb.AppendLine("  </div>");
            sb.AppendLine("</div>");
            sb.AppendLine("<!--/工具栏-->");
            sb.AppendLine("</form>");
            sb.AppendLine("</body>");
            sb.AppendLine("</html>");
            return sb.ToString();
        }


        /// <summary>
        /// 构造编辑aspx.cs文本
        /// </summary>
        /// <param name="tableEntity"></param>
        /// <param name="columnList"></param>
        /// <returns></returns>
        public string GenerateEditAspxCsText(TableEntity tableEntity, List<ColumnEntity> columnList,ConfigParameter configParameter)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("using System;");
            sb.AppendLine("using System.Data;");
            sb.AppendLine("using System.Collections.Generic;");
            sb.AppendLine("using System.Web;");
            sb.AppendLine("using System.Web.UI;");
            sb.AppendLine("using System.Web.UI.WebControls;");
            sb.AppendLine("using WeFrame.Core;");
            sb.AppendLine("using WeFrame.Core.Common;");
            sb.AppendLine("using WeFrame.Core.BLL;");
            sb.AppendLine("using WeFrame.Core.Model;");
            sb.AppendLine("using " + configParameter.NameSpaceTag + ".Model;");
            sb.AppendLine("using " + configParameter.NameSpaceTag + ".BLL;");
            sb.AppendLine("");
            sb.AppendLine("namespace "+GetNameSpace(configParameter.NameSpaceTag));
            sb.AppendLine("{");
            sb.AppendLine("    public partial class " + tableEntity.TableName + "Edit :  WeFrame.Core.UI.AdminPage");
            sb.AppendLine("    {");
            sb.AppendLine("        private string action;//操作类型");
            sb.AppendLine("        private int id = 0;");
            sb.AppendLine("        " + tableEntity.TableName + "BLL bll = new " + tableEntity.TableName + "BLL();");
            sb.AppendLine("        " + tableEntity.TableName + "Entity model;");
            sb.AppendLine("");
            sb.AppendLine("        protected void Page_Load(object sender, EventArgs e)");
            sb.AppendLine("        {");
            sb.AppendLine("");
            sb.AppendLine("            if (!Page.IsPostBack)");
            sb.AppendLine("            {");
            sb.AppendLine("                string action = WeFrameRequest.GetQueryString(\"action\");");
            sb.AppendLine("                HiddenFieldAction.Value = action;");
            sb.AppendLine("");
            sb.AppendLine("                ChkAdminLevel(\"" + tableEntity.TableName + "\", WeFrameEnums.ActionEnum.View.ToString()); //检查权限");
            sb.AppendLine("");
            sb.AppendLine("                ");
            sb.AppendLine("                ");
            sb.AppendLine("                if (action == WeFrameEnums.ActionEnum.Edit.ToString())");
            sb.AppendLine("                {");
            sb.AppendLine("                    if (!int.TryParse(Request.QueryString[\"Id\"] as string, out id))");
            sb.AppendLine("                    {");
            sb.AppendLine("                        JscriptMsg(\"传输参数不正确！\", \"back\");");
            sb.AppendLine("                        return;");
            sb.AppendLine("                    }");
            sb.AppendLine("");
            sb.AppendLine("                    if (!bll.Exists(id))");
            sb.AppendLine("                    {");
            sb.AppendLine("                        JscriptMsg(\"记录不存在或已被删除！\", \"back\");");
            sb.AppendLine("                        return;");
            sb.AppendLine("                    }");
            sb.AppendLine("");
            sb.AppendLine("                    HiddenFieldId.Value = id.ToString();");
            sb.AppendLine("");
            sb.AppendLine("                    ShowInformation(id);");
            sb.AppendLine("                ");
            sb.AppendLine("                }");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("            }");
            sb.AppendLine("        }");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("        #region 赋值操作=================================");
            sb.AppendLine("        private void ShowInformation(int id)");
            sb.AppendLine("        {");
            sb.AppendLine("            model = bll.GetModel(id);");
            foreach(ColumnEntity column in columnList)
            {

                if ("Id".Equals(column.Name) || "CreateBy".Equals(column.Name) || "CreateTime".Equals(column.Name) || "ModifyBy".Equals(column.Name) || "ModifyTime".Equals(column.Name))
                {
                    continue;
                }
                if (("nvarchar").Equals(column.Type) || ("varchar").Equals(column.Type)) 
                {
                    sb.AppendLine("            if(model."+column.Name+"!=null)");
                    sb.AppendLine("            {");
                    sb.AppendLine("                 TextBox" + column.Name + ".Text = model." + column.Name + ".ToString();");
                    sb.AppendLine("            }");
                    continue;
                }

                sb.AppendLine("            TextBox"+column.Name+".Text = model."+column.Name+".ToString();");
            }
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
            sb.AppendLine("");
            sb.AppendLine("        #region 增加操作=================================");
            sb.AppendLine("        private bool DoAdd()");
            sb.AppendLine("        {");
            sb.AppendLine("            "+tableEntity.TableName+"Entity model = new "+tableEntity.TableName+"Entity();");
            sb.AppendLine("            " + tableEntity.TableName + "BLL bll = new " + tableEntity.TableName + "BLL();");
    foreach (ColumnEntity column in columnList)
    {
        if (column.Name.Equals("Id")) continue;
        if (column.Name.Equals("CreateBy")) 
        {
            sb.AppendLine("            model." + column.Name + "=this.GetSystemAdmin().Id;");
            continue;
        }
        if (column.Name.Equals("CreateTime")) 
        {
            sb.AppendLine("            model." + column.Name + "=DateTime.Now;");
            continue;
        }
        if (column.Name.Equals("ModifyBy"))
        {
            sb.AppendLine("            model." + column.Name + "=model.CreateBy;");
            continue;
        }
        if (column.Name.Equals("ModifyTime"))
        {
            sb.AppendLine("            model." + column.Name + "=model.ModifyTime;");
            continue;
        }



        if (column.Type.Equals("int"))
        {
            sb.AppendLine("            model." + column.Name + "=int.Parse(TextBox" + column.Name + ".Text);");
        }
        else if (column.Type.Equals("tinyint"))
        {
            sb.AppendLine("            model." + column.Name  + "=byte.Parse(TextBox" + column.Name + ".Text);");   
        }
        else if (column.Type.Equals("datetime"))
        {
            sb.AppendLine("            model." + column.Name + "=DateTime.Parse(TextBox" + column.Name + ".Text);");   
        }
        else if (column.Type.Equals("nvarchar"))
        {
            sb.AppendLine("            model." + column.Name + "=TextBox" + column.Name + ".Text;");     
        }
        else
        {
            sb.AppendLine("            model." + column.Name + "=TextBox" + column.Name + ".Text;");     
        }
           
    }
            sb.AppendLine("");
            sb.AppendLine("            if (bll.Add(model) > 0)");
            sb.AppendLine("            {");
            sb.AppendLine("                return true;");
            sb.AppendLine("            }");
            sb.AppendLine("            return false;");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
            sb.AppendLine("");
            sb.AppendLine("        #region 修改操作=================================");
            sb.AppendLine("        private bool DoEdit(int id)");
            sb.AppendLine("        {");
            sb.AppendLine("            bool result = false;");
            sb.AppendLine("            model = bll.GetModel(id);");

    foreach (ColumnEntity column in columnList)
    {
        if (column.Name.Equals("Id"))continue;

        if (column.Name.Equals("CreateBy"))
        {
            continue;
        }
        if (column.Name.Equals("CreateTime"))
        {
            continue;
        }
        if (column.Name.Equals("ModifyBy"))
        {
            sb.AppendLine("            model." + column.Name + "=this.GetSystemAdmin().Id;");
            continue;
        }
        if (column.Name.Equals("ModifyTime"))
        {
            sb.AppendLine("            model." + column.Name + "=DateTime.Now;");
            continue;
        }

        if (column.Type.Equals("int"))
        {
            sb.AppendLine("            model." + column.Name + "=int.Parse(TextBox" + column.Name + ".Text);");
        }
        else if (column.Type.Equals("tinyint"))
        {
            sb.AppendLine("            model." + column.Name  + "=byte.Parse(TextBox" + column.Name + ".Text);");   
        }
        else if (column.Type.Equals("datetime"))
        {
            sb.AppendLine("            model." + column.Name + "=DateTime.Parse(TextBox" + column.Name + ".Text);");   
        }
        else if (column.Type.Equals("nvarchar"))
        {
            sb.AppendLine("            model." + column.Name + "=TextBox" + column.Name + ".Text;");     
        }
        else
        {
            sb.AppendLine("            model." + column.Name + "=TextBox" + column.Name + ".Text;");     
        }
        
    }
            sb.AppendLine("");
            sb.AppendLine("            if (bll.Update(model))");
            sb.AppendLine("            {");
            sb.AppendLine("               ");
            sb.AppendLine("                result = true;");
            sb.AppendLine("            }");
            sb.AppendLine("");
            sb.AppendLine("            return result;");
            sb.AppendLine("        }");
            sb.AppendLine("        #endregion");
            sb.AppendLine("");
            sb.AppendLine("        //保存");
            sb.AppendLine("        protected void ButtonSubmit_Click(object sender, EventArgs e)");
            sb.AppendLine("        {");
            sb.AppendLine("");
            sb.AppendLine("            if (HiddenFieldAction.Value == WeFrameEnums.ActionEnum.Edit.ToString()) //修改");
            sb.AppendLine("            {");
            sb.AppendLine("                ChkAdminLevel(\"" + tableEntity.TableName + "\", WeFrameEnums.ActionEnum.Edit.ToString()); //检查权限");
            sb.AppendLine("                id = int.Parse( HiddenFieldId.Value);");
            sb.AppendLine("                if (!DoEdit(this.id))");
            sb.AppendLine("                {");
            sb.AppendLine("                    JscriptMsg(\"保存过程中发生错误！\", \"\");");
            sb.AppendLine("                    return;");
            sb.AppendLine("                }");
            sb.AppendLine("                JscriptMsg(\"修改管理员信息成功！\", \""+tableEntity.TableName+"List.aspx\");");
            sb.AppendLine("            }");
            sb.AppendLine("            else if (HiddenFieldAction.Value == WeFrameEnums.ActionEnum.Add.ToString())//添加");
            sb.AppendLine("            {");
            sb.AppendLine("                ChkAdminLevel(\"ManagerList\", WeFrameEnums.ActionEnum.Add.ToString()); //检查权限");
            sb.AppendLine("                if (!DoAdd())");
            sb.AppendLine("                {");
            sb.AppendLine("                    JscriptMsg(\"保存过程中发生错误！\", \"\");");
            sb.AppendLine("                    return;");
            sb.AppendLine("                }");
            sb.AppendLine("                JscriptMsg(\"添加管理员信息成功！\", \""+tableEntity.TableName+"List.aspx\");");
            sb.AppendLine("            }");
            sb.AppendLine("        }");
            sb.AppendLine("    }");
            sb.AppendLine("}");


            return sb.ToString();

        }


        /// <summary>
        /// 构造编辑aspx.designer.cs文本
        /// </summary>
        /// <param name="tableEntity"></param>
        /// <param name="columnList"></param>
        /// <returns></returns>
        public string GenerateEditAspxDesignerCsText(TableEntity tableEntity, List<ColumnEntity> columnList,ConfigParameter configParameter)
        {
            StringBuilder sb = new StringBuilder();

            sb.AppendLine("//------------------------------------------------------------------------------");
            sb.AppendLine("// <自动生成>");
            sb.AppendLine("//     此代码由工具生成。");
            sb.AppendLine("//");
            sb.AppendLine("//     对此文件的更改可能会导致不正确的行为，并且如果");
            sb.AppendLine("//     重新生成代码，这些更改将会丢失。 ");
            sb.AppendLine("// </自动生成>");
            sb.AppendLine("//------------------------------------------------------------------------------");

            sb.AppendLine("namespace "+GetNameSpace(configParameter.NameSpaceTag)+" {");
            sb.AppendLine("");
            sb.AppendLine("");
            sb.AppendLine("    public partial class "+tableEntity.TableName+"Edit {");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// form1 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.HtmlControls.HtmlForm form1;");
            sb.AppendLine("");
            foreach (ColumnEntity column in columnList)
            {
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// TextBox"+column.Name+" 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.WebControls.TextBox TextBox"+column.Name+";");
            sb.AppendLine("");
            }
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// ButtonSubmit 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.WebControls.Button ButtonSubmit;");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// HiddenFieldAction 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.WebControls.HiddenField HiddenFieldAction;");
            sb.AppendLine("");
            sb.AppendLine("        /// <summary>");
            sb.AppendLine("        /// HiddenFieldId 控件。");
            sb.AppendLine("        /// </summary>");
            sb.AppendLine("        /// <remarks>");
            sb.AppendLine("        /// 自动生成的字段。");
            sb.AppendLine("        /// 若要进行修改，请将字段声明从设计器文件移到代码隐藏文件。");
            sb.AppendLine("        /// </remarks>");
            sb.AppendLine("        protected global::System.Web.UI.WebControls.HiddenField HiddenFieldId;");
            sb.AppendLine("    }");
            sb.AppendLine("}");

            return sb.ToString();
        }

        /// <summary>
        /// 获得表现层命名空间
        /// </summary>
        /// <param name="nameSpace"></param>
        /// <returns></returns>
        public static string GetNameSpace(string nameSpace)
        {
            return "WeFrame.Web.Admin." + nameSpace.Substring(nameSpace.IndexOf('.')+1);
        }

    }
}
